<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('',false)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all :props="{children:'children',label:'label'}">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%;font-size:16px">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>负责人</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id,false)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hAdd(data.id,true)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.children.length" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu></el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit?'编辑部门':'添加部门'"
      :visible.sync="daiLog"
      width="50%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      @close="hClose"
    >
      <Userdialog :id="curID" ref="diago" v-model="daiLog" :origin-list="originList" :is-edit="isEdit" @Ahuccess="Huccess" />
    </el-dialog>
  </div>
</template>
<script>
import Userdialog from './deptDialog.vue'
import { deleteDepartment, getDepartment } from '@/api/departments.js'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    Userdialog
  },
  data() {
    return {
      daiLog: false,
      list: [],
      originList: [],
      curID: '',
      isEdit: false

    }
  },
  created() {
    this.loadList()
  },
  methods: {
    hClose() {
      this.$refs.diago.resetForm()
    },
    hDel(id) {
      this.$confirm('您确定删除此部门吗?').then(() => {
        deleteDepartment(id)
      }).catch(err => err)
    },
    async loadList() {
      const res = await getDepartment()
      console.log(res)
      this.originList = res.data.depts
      // if (this.isEdit) {
      //   this.originList = this.originList.filter((item) => item === this.form.code)
      // }
      // console.log(this.originList)
      res.data.depts.shift()
      this.list = tranListToTreeData(res.data.depts)
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    Huccess() {
      this.daiLog = false
      this.loadList()
    },
    hAdd(id, edit) {
      this.curID = id
      this.isEdit = edit
      if (edit) {
        this.$nextTick(() => {
          this.$refs.diago.hGetOne()
        })
      } else {
        this.$nextTick(() => {
          this.$refs.diago.form = {
            name: '', // 部门名称
            code: '', // 部门编码
            manager: '', // 部门管理者
            introduce: '' // 部门介绍
          }
        })
      }
      this.daiLog = true
    }
  }
}
</script>

